$(function() {
    $("#loginsubmit").click(function() {
        var options = {
            url: "http://127.0.0.1:8888/users/login",
            type: "POST",
            dataType: "json",
            data: {
                username: username.value,
                password: password.value
            }

        }

        $.ajax(options)
            .done(function(data) {
                render(data);
            })

        function render(data) {
            switch (data.code) {
                case 0:
                    alert(data.message);
                    break;

                case 1:
                    console.log(data);
                    alert(data.message);
                    setTimeout(function() {
                        location.href = "./homePage.html";
                    })

                    //登录成功之后获取存储用得ID
                    setCookie("userid", data.user.id)

                    setCookie("token", data.token, {
                        expires: 1
                    });

                    break;
            }



        }


    })
})